<template>
  <div style="padding: 20px">
    <el-form ref="form" label-width="100px">
      <el-form-item label="课程标题">
        <el-input v-model="course.title"/>
      </el-form-item>
      <el-form-item label="课程讲师">
        <el-select v-model="course.teacherId" placeholder="请选择课程讲师">
          <el-option
            v-for="item in teachers"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程类别">
        <el-select v-model="course.subjectParentId" placeholder="请选择课程父级别分类" @change="handleLevelOneSubjectChange">
          <el-option
            v-for="item in levelOneSubject"
            :key="item.id"
            :label="item.title"
            :value="item.id"/>
        </el-select>
        <el-select v-model="course.subjectId" placeholder="请选择课程次级分类">
          <el-option
            v-for="item in levelTwoSubjects"
            :key="item.id"
            :label="item.title"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="课程总课时">
        <el-input-number v-model="course.lessonNum" :min="1" controls-position="right" />
      </el-form-item>
      <el-form-item label="课程简介">
        <tinymce :height="300" v-model="course.description"/>
      </el-form-item>

      <el-form-item label="课程封面">
        <el-upload
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          :action="uploadUrl"
          class="avatar-uploader">
          <img v-if="course.cover" :src="course.cover" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon"/>
        </el-upload>
      </el-form-item>
      <el-form-item label="课程的价格">
        <el-input-number v-model="course.price" :min="0" controls-position="right" />  元
      </el-form-item>

    </el-form>

    <div style="text-align: center">
      <el-button-group>
        <el-button :disabled="flag" type="primary" @click="next">保存并下一步<i class="el-icon-arrow-right el-icon--right"/></el-button>
      </el-button-group>
    </div>
  </div>
</template>
<script>
import teacherApi from '@/api/teacher'
import subjectApi from '@/api/subject'
import Tinymce from '@/components/Tinymce'
import courseApi from '@//api/course'
export default {
  components: {
    Tinymce
  },
  data() {
    return {
      flag: false,
      course: {
        subjectId: ''
      },
      teachers: [],
      levelOneSubject: [],
      levelTwoSubjects: [],
      uploadUrl: 'http://localhost:8120/admin/oss/file/upload?module=cover'
    }
  },
  created() {
    teacherApi.list().then(r => {
      this.teachers = r.data.items
    })

    subjectApi.listNestedSubjects().then(r => {
      this.levelOneSubject = r.data.items
      // 如果父组件中courseId属性存在，代表本次已经提交过课程信息，查询回显

      // 获取路径中的id,如果存在，代表是更新跳转过来的，也需要回显数据
      // if (this.$route.params.id) {
      //   this.$parent.courseId = this.$route.params.id

      // this.$parent.active = Number(this.$route.query.active)
      //  刷新浏览器地址栏
      // this.$router.push({ query: {}})
      // this.$router.push(this.$route.path) // 接受使用的参数后让地址栏重新访问 编辑的路由地址 去掉查询参数
      // }

      if (!this.$parent.courseId) {
        if (this.$route.query.active) {
          this.$parent.active = Number(this.$route.query.active)
        }
        this.$parent.courseId = this.$route.params.id
      }

      if (this.$parent.courseId) {
        courseApi.getCourseInfo(this.$parent.courseId).then(r => {
          this.course = r.data.item
          this.levelOneSubject.forEach((levelOneSubject) => {
            if (levelOneSubject.id === this.course.subjectParentId) {
              this.levelTwoSubjects = levelOneSubject.children
              return
            }
          })
        })
      }
    })
  },
  methods: {
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      if (res.code === 20000) {
        this.course.cover = res.data.path
        this.$forceUpdate()
      } else {
        this.$message.error(res.message)
      }
      // this.teacher.avatar = res.data.path
      // //  强制vue重新渲染
      // this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    next() {
      /*
      * 此页面以后在form页面中引入使用
      * form 就是当前页面的父组件
      * info页面就是子组件
      * */
      this.flag = true

      if (this.$parent.courseId) {
        // 更新
        this.update()
      } else {
        // 新增
        this.save()
      }
    },
    save() {
      courseApi.saveCourseInfo(this.course).then(r => {
        this.$message.success('保存课程基本信息成功')
        this.$parent.courseId = r.data.id // 将保存课程的id存在父组件中
        // 成功后跳转到下一步
        this.$parent.active = 1
      })
    },
    update() {
      courseApi.updateCourseInfo(this.course, this.$parent.courseId).then(r => {
        this.$message.success('更新课程基本信息成功')
        // 成功后跳转到下一步
        this.$parent.active = 1
      })
    },
    // 父级别课程分类，内容改变的监听函数
    handleLevelOneSubjectChange(val) { // val代表选中的一级分类的id
      // 清空二级分类集合
      this.course.subjectId = ''
      // 根据一级分类的id查找一级分类对象，获取他的children属性 就可以得到二级分类集合
      this.levelOneSubject.forEach((levelOneSubject) => {
        if (levelOneSubject.id === val) {
          this.levelTwoSubjects = levelOneSubject.children
          return
        }
      })
    }
  }
}
</script>
<style >
.tinymce-container {
  position: relative;
  line-height: normal;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 356px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 356px;
  height: 178px;
  display: block;
}
</style>

